#include "050.h"
//extern const char*cont[4];
void head(struct login*T1)
{
	printf("\033c");
	printf("\t\t******************XXX系统******************\n");
	printf("\t\t****账号:%-7s**********权限:%10s*****\n",T1->account,cont[T1->leve]);
	printf("\t\t*******************************************\n");
}
//
void login_1(struct login*T1)
{
	head(T1);
	char x;
	while(1)
	{
		head(T1);
		printf("\t\t****按y登录\n");
		printf("\t\t****按z注册\n");
		printf("\t\t****其他任意键退出\n");
		printf("\t\t请输入：");
		x=getchar();
		getchar();
		if(x=='y')
			login_2_1(T1);
		else if(x=='z')
			login_2_2(T1);
		else
			return;
	}
}
//////////////////登陆
void login_2_1(struct login*T1)
{
	head(T1);
	printf("\t\t****请输入账号：");
	scanf(" %s",T1->account);
	getchar();
	if(login_verify(T1,1)==-1)
	{
		printf("\t\t****此账户不存在\n");
		printf("\t\t****任意键值返回\n\t\t");
		getchar();
		return;
	}
	printf("\t\t****请输入密码：");
	scanf(" %s",T1->password);
	getchar();
	if(login_verify(T1,1)!=0)
	{
		printf("\t\t****密码错误\n");
		printf("\t\t****任意键值确认\n\t\t");
		getchar();
		return;
	}
	


	return;
}

void login_2_2(struct login*)
{
	return;
}
void login_2_3(struct login*)
{
	return;
}
//用户验证(-2:密码错误 -1:用户名不存在 0:无效值 1:用户名存在 2:用户名密码正确)
int login_verify(struct login*T1,int x)
{
	FILE*p=NULL;
	if((p=fopen("./000.txt","r"))==NULL)
	{
		perror("\t\t账户信息文件打开失败");
		printf("\t\t****任意键值确认\n\t\t");
		getchar();
		return -2;
	}
	struct login T2={"","",0};
	while(fscanf(p,"账号:%7s 密码:%20s\n 权限:%2d\n",T2.account,T2.password,&T2.leve)!=EOF)
	{
		if(T2.account==T1->account)
		{
			if(x==1){fclose(p);return 1;}
			if(x==2)
				if(T2.password==T1->password){fclose(p);return 2;}
				else{fclose(p);return -2;}
		}
	}
	if(x==1){fclose(p);return -1;}
	if(x==2){fclose(p);return -2;}
}
